package com.his.dictcode.controller;

import com.his.config.annotation.CurrentUser;
import com.his.dictcode.entity.DictCodeType;
import com.his.dictcode.service.DictCodeTypeService;
import com.his.dictcode.vo.DictCodeTypeVo;
import com.his.login.entity.DictEmployee;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author changlong
 * @date 2020/2/4 0004 - 17:59
 */

/**
 *1、在vo前面加上@Validated注解就可以对vo上加了注解判空的进行验证
 * 2、RequestMapping的地址命名尽量简短 /api/+主功能+/每个方法上命名的（参见我修改的）
 * 3、控制器每个方法都加上try catch 程序的严谨性
 *
 */
@RestController
@RequestMapping("/api/code_type")
public class DictCodeTypeController extends ResultBaseController {
    private  final org.slf4j.Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DictCodeTypeService dictCodeTypeService;

    @GetMapping(value="/query_all.do")
    public ResultUtil queryAll(@CurrentUser DictEmployee dictEmployee,String keyWords){
        try {
            List<DictCodeType> resultlist = dictCodeTypeService.queryAll(keyWords);
            return this.success("",resultlist);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return null;
        }
    }

    @PostMapping("/insert.do")
    public ResultUtil insertAll(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated DictCodeTypeVo dictCodeTypeVo){
        try {
            dictCodeTypeService.insertAll(dictCodeTypeVo);
            return this.success("保存成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @PostMapping(value="/update.do")
    public ResultUtil updateById(@RequestBody @Validated DictCodeTypeVo dictCodeTypeVo){
        try {
            dictCodeTypeService.updateById(dictCodeTypeVo);
            return this.success("更新成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    @RequestMapping("/delete.do")
    public ResultUtil deleteById(@Validated DictCodeTypeVo dictCodeTypeVo){
        try {
            dictCodeTypeService.deleteById(dictCodeTypeVo);
            return this.success("删除成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping("/get_by_id.do")
    public ResultUtil selectById(@CurrentUser DictEmployee dictEmployee,String codeTypeId){
        try {
            return this.success("", dictCodeTypeService.selectById(codeTypeId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
}
